
const Article = require('../model/article')
const Tab = require('../model/tab')

/**
 * 题目中间件
 */
module.exports = {
    /**
     * 获取全部题目
     */
    getList: (req, res, next) => {
        Article.getList().then(results => {
            req.articles = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取我的选题
     */
    getListByTid: (req, res, next) => {
        let tid = req.params.tid
        Article.getListByTid(tid).then(results => {
            req.articles = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取我的选题数
     */
    getArticleCount: (req, res, next) => {
        let tid = req.params.tid
        Article.getArticleCount(tid).then(results => {
            req.myArticleCount = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取我的选题数
     */
    getStudentsList: (req, res, next) => {
        let id = req.params.id
        Article.getStudentsList(id).then(results => {
            req.students = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取指定类目下的题目列表
     */
    getListByCategoryId: (req, res, next) => {
        let id = req.params.id
        Article.getListByCategoryId(id).then(results => {
            req.articles = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取指定关键词的题目列表
     */
    getListBykeywrod: (req, res, next) => {
        let keyword = req.query.keyword
        Article.getListBykeywrod(keyword).then(results => {
            req.articles = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取指定题目的详情
     */
    getArticleById: (req, res, next) => {
        let id = req.params.id
        Article.getArticleById(id).then(results => {
            req.article = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取指定题目的标签列表
     */
    getTabs: (req, res, next) => {
        let id = req.params.id
        Tab.getListByArticleId(id).then(results => {
            req.tabs = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 上一篇题目
     */
    getPrev: (req, res, next) => {
        let id = req.params.id
        Article.getPrevArticle(id).then(results => {
            req.prev = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 下一篇题目
     */
    getNext: (req, res, next) => {
        let id = req.params.id
        Article.getNextArticle(id).then(results => {
            req.next = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取总博文数
     */
    getCount: (req, res, next) => {
        Article.getCount(req.query.category_id, req.query.hot, req.query.code).then(results => {
            req.articleCount = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 获取指定页的题目列表
     */
    getPage: (req, res, next) => {
        Article.getPage(res.start, res.size, req.query.category_id, req.query.hot, req.query.code).then(results => {
            req.pageList = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 设置热门推荐
     */
    setHot: (req, res, next) => {
        let { id, hot } = req.query
        Article.setHot(id, hot).then(results => {
            req.affectedRows = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    
    /**
     * 添加题目
     */
    add: (req, res, next) => {
        let { title, major, content, hot, category_id, tid } = req.body
        let article = {
            title: title,
            major: major,
            content: content,
            hot: hot ? 1 : 0,
            category_id: category_id,
            tid: tid,
            uid: 0,
            code: 9
        }

        Article.add(article).then(results => {
            req.insertId = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 删除题目
     */
    del: (req, res, next) => {
        let { id } = req.query
        Article.del(id).then(results => {
            req.affectedRows = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 编辑题目(有审核)
     */
    edit: (req, res, next) => {
        let { title, content, hot, category_id, major, id, tid, code, uid, checkIsOrNot} = req.body
        if(checkIsOrNot==1&&code==9){
            code = 0
        }else if(code==1){
            code = code
        }else if(code==2){
            code = 9
        }else if(code==1&&uid!=0) {
            code = 3
        }
        let article = {
            title: title,
            content: content,
            hot: hot ? 1 : 0,
            category_id: category_id,
            major: major,
            id: id,
            tid: tid,
            code: code,
            uid: uid
        }
        Article.edit(article).then(results => {
            req.affectedRows = results
            next()
        }).catch(err => {
            next(err)
        })
    },
    /**
     * 反选学生
     */
    checkUid: (req,res,next)=>{
        let uid = req.body
        Article.checkUid(uid).then(results => {
            req.isIn = results
            next()
        }).catch(err => {
            next(err)
        })
    },

    setStudent: (req, res, next) => {
        let { id, code, uid} = req.body
        if(code==1&&uid!=0){
            code = 3
        }else if(code==1&&uid==0){
            code = 1
        }else {
            code = code
        }
        let article = {
            id: id,
            code: code,
            uid: uid
        }
        Article.setStudent(article).then(results => {
            req.affectedRows = results
            next()
        }).catch(err => {
            next(err)
        })
    }

}